﻿#include <iostream>
#include <list>
#include <fstream>
#include <string>
#include <sstream>
#include <queue>
#include "stdafx.h"
#include <sys/stat.h>


using namespace std;

struct OrderItem
{
        public: 
        int itemNumber;
        int quantity;
};

class OrderManagement
{
private:
	fstream OrderFile;
	int orderNumber;

public:
	
	//Summary: (Default Constructor) Locates an unused file number and
	//         then creates that file.
	//Inputs:  (Nothing)
	//Returns: (Nothing)
	OrderManagement() 
	{
		orderNumber = FindFreeOrderNo();
		// Convert orderNumber into a string
		stringstream s;
		string fileName;

		s << orderNumber;
		fileName = s.str();

		// Create file for writing
		OrderFile.open(fileName.c_str(), ios::app);
	}


	//Summary: (Constructor) Create new file with name as orderNumber
	//Inputs: orderNumber = Name of file created;
	//Returns: (Nothing)
	OrderManagement (int orderNumber)
	{
		this->orderNumber = orderNumber;
		// Convert orderNumber into a string
		stringstream s;
		string fileName;

		s << orderNumber;
		fileName = s.str();

		// Create file for writing
		OrderFile.open(fileName.c_str(), ios::app);
	}

	//Summary: (Destructor) Close file after it's done
	//Returns: (Nothing)
	~OrderManagement()
	{
		OrderFile.close();
	}


	//Summary: Returns the order number.
	//Inputs:  (Nothing)
	//Returns: The current order number.
	int GetOrderNumber() 
	{
		return orderNumber;
	}

	//Summary: Remove order file
	//Inputs: orderNumber = The file name to delete
	//Returns: Successful or failure of the file delete
	bool DeleteFile (int orderNumber)
	{
		// Convert orderNumber into a string
		stringstream s;
		string fileName;

		s << orderNumber;
		fileName = s.str();
		// Return the result of the deletes
		if(remove(fileName.c_str()) == 0)
			return true;
		else
			return false;
	}

	//Summary: Read the order from the file
	//Inputs: orderNumber = File name to read
	//Returns: List of items
	list<OrderItem> ReadFile (int orderNumber)
	{
		stringstream s;
		string fileName;
		list<OrderItem> readOrder;
		
		int size=100;
		char str[100];
		
		s << orderNumber;
		fileName = s.str();
		OrderFile.open(fileName.c_str(), ios::in);
		if(!OrderFile)
		{
		  cout << "Unable to open file";
		  exit(1);
		}
		//OrderFile.read(str, size);
		cout << "Reading data from file...\n" << str << endl;
		return readOrder;
	}


	//Summary: This function generates an order number by finding the first
	//         available hole from the current list of file names.
	//Inputs: (none)
	//Returns: an order number to use
	int FindFreeOrderNo ()
	{
		int counter = 0;
		stringstream filename;
		struct stat stFileInfo;
		do{
			counter++;
			filename << counter;
		}while(stat(filename.str().c_str(),&stFileInfo)==0);

		return counter;
	}

	//Summary: Write order to file
	//Inputs: (Nothing)
	//Returns: (Nothing)
	void WriteFile(list<OrderItem> orderItems)
	{
		list<OrderItem>::iterator it;
        for ( it=orderItems.begin(); it != orderItems.end(); it++ )
        {
                OrderFile << it->itemNumber << " " << it->quantity << "\r\n";
        }
	}

};